# # --------- import和get_asgi_application()的位置顺序不能变!
# import os
# from django.core.asgi import get_asgi_application
#
# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'websocket_my.settings')
# django_asgi_app = get_asgi_application()
# # ------- 用console调试的时候必须在最前面, 不然部署的时候报错!


import redis   # 导入redis 模块

pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(connection_pool=pool)

EXPIRATION_TIME = 5


class room_id_to_user_list:
    # 根据房间id, 查询该房间的user_list
    def __init__(self):
        self.r = r

    @staticmethod
    def get_room_name(room_id):
        key_name = f'room_{room_id}'
        r.expire(key_name, EXPIRATION_TIME)  # 超时时间

        return key_name

    @staticmethod
    def add_user_to_user_list(room_id, user_id, ex=EXPIRATION_TIME):
        room_name = room_id_to_user_list.get_room_name(room_id)

        user_list = r.lrange(room_name, 0, -1)
        if str(user_id) not in user_list:
            ret = r.lpush(room_name, user_id)

            r.expire(room_name, ex)  # 超时时间
            return ret
        else:
            print(f"--- 尝试添加已有用户[{user_id}]! ---")
            return 0

    @staticmethod
    def remove_user_from_user_list(room_id, user_id):
        room_name = room_id_to_user_list.get_room_name(room_id)
        r.lrem(room_name, 0, user_id)

    @staticmethod
    def show_user_list(room_id):
        room_name = room_id_to_user_list.get_room_name(room_id)
        ret =  r.lrange(room_name, 0, -1)
        print(ret)
        return ret


class user_id_compare_channel:
    # 用户id 和 用户channel 对照操作类
    def __init__(self):
        self.r= r

    @staticmethod
    def get_key_name(room_id):
        key_name = f'room_{room_id}:id_compare_channel'

        return key_name

    @staticmethod
    def save_room_user_channel(room_id, user_id, user_channel, ex=EXPIRATION_TIME):
        # 各个房间的{用户id: channel}对照关系
        key_name = user_id_compare_channel.get_key_name(room_id)
        ret = r.hset(key_name, user_id, user_channel)
        r.expire(key_name, ex)       # 超时时间

        return ret

    @staticmethod
    def get_user_channel_by_user_id(room_id, user_id):
        # 各个房间的{用户id: channel}对照关系
        key_name = user_id_compare_channel.get_key_name(room_id)
        ret = r.hget(key_name, user_id)
        return ret

    @staticmethod
    def remove_channel(room_id, user_id):
        key_name = user_id_compare_channel.get_key_name(room_id)
        ret = r.hdel(key_name, user_id)
        return ret

    @staticmethod
    def show_all(room_id):
        key_name = user_id_compare_channel.get_key_name(room_id)
        return r.hgetall(key_name)


def init_redis_by_room_group_name(room_group_name):
    """
    在重启服务时, 清空redis中的缓存数据(各房间用户清单和黑名单).
    正式版应该注意及时将redis中的数据保存至数据库中.
    """
    keys = r.keys("room*")
    for k in keys:
        r.delete(k)

    # 清空黑名单
    keys = r.keys("blacklist_*")
    for k in keys:
        r.delete(k)

    return 1


def add_someone_to_blacklist(room_id, user_id, ex=EXPIRATION_TIME):
    name = 'blacklist_' + room_id
    value = user_id

    ret = r.sadd(name, value)
    r.expire(name, ex)
    return ret


def is_black(room_id, user_id):
    name = 'blacklist_' + room_id
    value = user_id
    if r.sismember(name, value):
        return 1
    else:
        return 0

# add_someone_to_blacklist('room_1', 'sb', ex=180)
# is_black('room_1', 'sb')

if __name__ == '__main__':
    # EXPIRATION_TIME = 3
    #
    # room_id = 111
    # user_id = 223
    # user_channel = "channel_2"
    #
    # user_id_compare_channel.get_key_name(room_id)
    # user_id_compare_channel.save_room_user_channel(room_id, user_id, user_channel)
    # user_id_compare_channel.get_user_channel_by_user_id(room_id, user_id)
    # user_id_compare_channel.show_all(room_id)
    #
    #
    # # --------- 特定房间下的用户列表 -------
    # room_id_to_user_list.add_user_to_user_list(room_id, user_id)
    # room_id_to_user_list.get_room_name(room_id)
    # room_id_to_user_list.show_user_list(room_id)


    name = 's1'
    value = 'bode1'

    r.sadd(name, value)
    r.scard(name)
    r.smembers(name)
    r.sismember(name, value)

    # r.lpush(name, value)
    # r.lrange(name, 0, -1)
    # r.append()
    # r.set(name, value, ex=60)
    # r.get(name)

